/**
 * 评论数据组件
 */
define('pc/js/mod/posts/posts-show-relative', [
    'utils/XTemplate',
    'utils/helpers',
    'utils/fetchEnhancer'
], function(XTemplate, h, fetch){

function Relative(configs /* = {wrapperId, tags, postId}*/) {
    this.html =
'<% var list = data.list, h=data.helpers; for(var i=0,len=list.length; i<len; i++){ %>' +
'<div class="read-detail-more-item">' +
    '<a class="read-detail-more-item-right" target="_blank" href="<%= h.U("posts", {id: list[i].id}, ".html") %>">' +
        '<img src="<%= list[i].thumb %>">' +
    '</a>' +
    '<div class="read-detail-more-item-left">' +
        '<a class="read-detail-more-item-title" target="_blank" href="<%= h.U("posts", {id: list[i].id}, ".html") %>"><%= list[i].title %></a>' +
        '<div class="read-detail-more-item-desc"><%= list[i].brief %></div>' +
    '</div>' +
'</div>' +
'<% } %>';

    this.template = null;
    this.configs = configs;

    this.init();
    this.fetchList();
}
Relative.prototype = {
    constructor: Relative,
    init: function() {
        this.template = new XTemplate();
        this.template.compile(this.html);
    },
    fetchList: function() {
        var _self = this;

        fetch('/api/post/relative?post_id=' + this.configs.postId + '&tags=' + encodeURIComponent(this.configs.tags), {
            'method': 'GET'
        }).then(function(json){
            if(0 === json.status) {
                _self.renderList(json.data.data);
            }
        })
    },
    renderList: function(list) {
        if(!list) {
            return;
        }

        var html = this.template.run({
            list: list,
            helpers: h
        });

        document.getElementById(this.configs.wrapperId).innerHTML = html;
    }
};

return Relative;

});
